package
{
	import com.godpaper.mqtt.as3.utils.Base64;
	import com.godpaper.mqtt.as3.core.Log;
	import com.godpaper.mqtt.as3.core.MQTTEvent;
	import com.godpaper.mqtt.as3.impl.MQTTSocket;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.external.ExternalInterface;
	
	public class AS3_MQTT extends Sprite
	{
		private var mqtt:MQTTSocket = null;
		
		public function AS3_MQTT()
		{
			super();
			
			Log.setDebug(true);
						
			addEventListener(Event.ADDED_TO_STAGE,onAddToStage);
		}
		
		private function addCallback():void
		{
			ExternalInterface.addCallback("createMQTT",createMQTT);
			ExternalInterface.addCallback("connectMQTT",connectMQTT);
			ExternalInterface.addCallback("removeMQTT",removeMQTT);
			ExternalInterface.addCallback("subscribe",subscribe);
//			ExternalInterface.addCallback("publish",publish);
			ExternalInterface.addCallback("publishBase64",publishBase64);
			ExternalInterface.addCallback("unsubscribe",unsubscribe);
			
			ExternalInterface.call("onMQTTLoaded");
		}
		
		private function onAddToStage(e:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE,onAddToStage);
			
			addCallback();
		}
		
		public function createMQTT(clientId:String):void
		{
			removeMQTT();
			
			mqtt = new MQTTSocket();
			
			mqtt.clientid = clientId;
			
			mqtt.addEventListener(MQTTEvent.CONNECT, onConnect); 
			mqtt.addEventListener(MQTTEvent.CLOSE, onClose); 
			mqtt.addEventListener(MQTTEvent.ERROR, onError); 
//			mqtt.addEventListener(MQTTEvent.MESSGE, onMessage); 
			mqtt.addEventListener(MQTTEvent.MESSGE, onMessageBase64);
		}
		
		public function removeMQTT():void
		{
			if(mqtt != null)
			{
				
				mqtt.removeEventListener(MQTTEvent.CONNECT, onConnect); 
				mqtt.removeEventListener(MQTTEvent.CLOSE, onClose); 
				mqtt.removeEventListener(MQTTEvent.ERROR, onError); 
//				mqtt.removeEventListener(MQTTEvent.MESSGE, onMessage); 
				mqtt.removeEventListener(MQTTEvent.MESSGE, onMessageBase64);
				mqtt.close();
				
			}
		}
		
		public function connectMQTT(host:String,port:int):void
		{
			if(mqtt != null)
			{
				mqtt.connect(host,port);
			}
		}
		
		public function subscribe(topics:Array,qoss:Array,qos:int):void
		{
			if(mqtt != null)
			{
				mqtt.subscribe(topics,qoss,qos);
			}
		}
		
		public function unsubscribe(topics:Array, qos:int):void
		{
			if(mqtt != null)
			{
				mqtt.unsubscribe(topics,qos);
			}
		}
		
		public function publish(topicname:String, message:String, qos:int=0, retain:int=0):void
		{
			if(mqtt != null)
			{
				mqtt.publish(topicname,message);
			}
		}
		
		private function publishBase64(topicname:String, message:String, qos:int=0, retain:int=0):void
		{
			if(mqtt != null)
			{
				mqtt.publish(topicname,Base64.decode(message));
			}
		}
		
		private function onConnect(event:MQTTEvent):void
		{
			
			ExternalInterface.call("onMQTTConnect");
		}
		
		private function onClose(event:MQTTEvent):void
		{
			ExternalInterface.call("onMQTTClose");
		}
		
		private function onError(event:MQTTEvent):void
		{
			ExternalInterface.call("onMQTTError");
		}
		
		private function onMessage(event:MQTTEvent):void
		{
			ExternalInterface.call("onMQTTMessage",event.message);
		}
		
		private function onMessageBase64(event:MQTTEvent):void
		{
			//回调给出的是base64
			ExternalInterface.call("onMQTTMessageBase64",Base64.encode(event.message));
		}
				
	}
}